import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);
import ElementUI from 'element-ui';

import Login from 'components/admin/login.vue';
import Control from 'components/admin/control_panel.vue';
import AddUser from 'components/admin/add_user.vue';
import AddUserExcel from 'components/admin/add_user_excel.vue'
import ScanUser from 'components/admin/scan_user.vue';
import ScanJob from 'components/admin/scan_job.vue';
import AddTitle from 'components/admin/add_title.vue';
import AddRecordSingle from 'components/admin/add_record_single.vue';
import AddRecordExcel from 'components/admin/add_record_excel.vue';
import ScanRecord from 'components/admin/scan_record.vue';
import Personal from 'components/admin/personal.vue';
import SiteSetting from 'components/admin/site_setting.vue';

let login_to_route_name = '';
let router = new Router({
	routes: [
		{ path: '(/login)?', name: 'login', component: Login },
		{ 
			path: '/control', 
			name: 'control', 
			component: Control,
			children: [
				{ path: '/control(/add_user)?', name: 'add_user', component: AddUser },
				{ path: '/control/add_user_excel', name: 'add_user_excel', component: AddUserExcel },
				{ path: '/control/scan_user', name: 'scan_user', component: ScanUser },
				{ path: '/control/scan_job', name: 'scan_job', component: ScanJob },
				{ path: '/control/add_title', name: 'add_title', component: AddTitle },
				{ path: '/control/add_record_single', name: 'add_record_single', component: AddRecordSingle },
				{ path: '/control/add_record_excel', name: 'add_record_excel', component: AddRecordExcel },
				{ path: '/control/scan_record', name: 'scan_record', component: ScanRecord },
				{ path: '/control/personal', name: 'personal', component: Personal },
				{ path: '/control/site_setting', name: 'site_setting', component: SiteSetting },
			]
		}
	]
});
let checkLogin = (callback, show_error = true) => {
	Vue.prototype.$axios.get('/admin/isLogin').then(res => {
		if(res.s != 1 && show_error) {
			ElementUI.Message.error(res.m || '未知异常');
		}
		callback(res.s);
	}, () => {
		callback('net_error');
	});
}
let login_name = 'login';
router.beforeEach((to, from, next) => {
	let to_type = to.params.type;

	if(to_type == 'no_check') {
		next();
	}else {
		if(from.name && to.name == login_name || (to_type && to_type == 'check_login')) {
		//logout or check_login fail
			login_to_route_name = from.name;
			next();
		}else {
			checkLogin(res => {
				if(res == 1) {
					if(to.name == login_name || (to.name=='scan_record' && to_type && to_type == 'login')) {
						//refresh login or input to login or login
						if(!login_to_route_name || login_to_route_name == login_name){
							login_to_route_name = 'scan_record';
						}
						let t_login_to_route_name = login_to_route_name;
						login_to_route_name = '';
						if(t_login_to_route_name == to.name) {
							next();
						}else {
							next({name:t_login_to_route_name, params:{type:'no_check'}});
						}
					}else {
						next();
					}
				}else if(res == 'Lg000') {
					if(to.name == login_name) {
						next();
					}else {
						login_to_route_name = to.name;
						next({name:login_name, params:{type:'check_login'}});
					}
				}
			}, to.name != login_name);
		}
	}
})
export default router;